package com.ay.weixin.fdc.boss.spage.event;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;

import org.apache.velocity.VelocityContext;
import org.lizar.config.Config;
import org.lizar.config.keys.Keys;
import org.lizar.controller.Event;
import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Container;
import org.lizar.util.FileTool;
import org.lizar.util.StringHelper;
import org.lizar.util.VelocityHelper;

import com.ay.weixin.fdc.admin.customer.domain.Customer;
import com.ay.weixin.fdc.admin.superadmin.domain.SuperAdmin;
import com.ay.weixin.fdc.boss.customer.event.CustomerLogin;
import com.ay.weixin.fdc.boss.spage.db.IbatisSpageDao;
import com.ay.weixin.fdc.boss.spage.domain.SPage;

public class SPageIndex extends Event {

	private IbatisSpageDao spage_dao;
	
	@Override
	public void init_property() throws Exception {
		spage_dao=Container.get(IbatisSpageDao.class);
	}

	@Override
	public String default_handling_path() {
		return "/spage/page/*";
	}

	@Override
	public void handle(EventLoader el) throws ServletException, IOException {
		Customer cus=(Customer)el.getSessionAttr("customer");
		if(cus==null){
			el.response_to_root(CustomerLogin.login_path());
		}else{
			try {
				if(StringHelper.equals(el.queryPath(2),"")){
					view_page_list(el,cus.getWxprojectID());
				}else if(StringHelper.equals(el.queryPath(2),"view")){
					view(el,cus.getWxprojectID());
				}else if(StringHelper.equals(el.queryPath(2),"update")){
					update(el,cus.getWxprojectID());
				}else{
					view_page_list(el,cus.getWxprojectID());
				}
			} catch (SQLException e) {
				e.printStackTrace();
				view_page_list(el,cus.getWxprojectID());
			}
		}
	}
	
	
	private void view_page_list(EventLoader el,long wxproject_id) throws IOException, ServletException{
		List<SPage> splist=null;
		String msg=el.getAttr("msg",null);
		try {
			splist=spage_dao.getAll(wxproject_id);
		} catch (SQLException e) {
			e.printStackTrace();
			msg=el.i18("spage.db.failure");
			
		}
		el.setAttr("splist", splist);
		el.setAttr("msg", msg);
		el.setAttr("rand", Math.random());
		el.template("/WEB-INF/template/boss/spage/index.vm");
	}
	
	private void view(EventLoader el,long wxproject_id) throws IOException, ServletException, SQLException{
		long id=el.getLong("id");
		SPage spage=spage_dao.get(id);
		if(spage==null){
			el.setAttr("msg", el.i18("spage.page.not.exists"));
			view_page_list(el,wxproject_id);
			return;
		}

		el.setAttr("spage",spage);
		
		el.setAttr("id",el.getStr("id"));
		
		el.template("/WEB-INF/template/boss/spage/view.vm");
	}
	
	private void update(EventLoader el,long wxproject_id) throws IOException, ServletException, SQLException{
		String content=el.getStr("content");
		String msg=el.getAttr("msg",null);
		long id=el.getLong("id",-1);
		if(id==-1){
			view_page_list(el,wxproject_id);
			return;
		}
		
		SPage spage=spage_dao.get(id);
		if(spage==null){
			msg=el.i18("spage.page_record.notfound");
			view_page_list(el,wxproject_id);
			return;
		}
		
		if(spage.getPath().lastIndexOf(".")==-1){
			msg=el.i18("spage.postfix.notcorrect");
			view_page_list(el,wxproject_id);
			return;
		}
		
		spage.setLast_update_time(System.currentTimeMillis());
		spage.setBody(content);
		spage_dao.update(spage);
		
		String page=spage.getPath().substring(0,spage.getPath().lastIndexOf("."))+".vm";
		VelocityContext vc=new VelocityContext();
		vc.put("body", content);
		String output=VelocityHelper.read_msg(el.getRealPath(page), vc);
		
		FileTool.write_to_file(output, new File(el.getRealPath(spage.getPath())),"utf-8");
		
		
		
		if(msg==null){
			msg=el.i18("spage.update.sucess");
		}
		el.setAttr("msg", msg);
		el.setAttr("rand", Math.random());
		view(el,wxproject_id);
		
	}

}
